Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    houseName: '',
    repairName: '',
    id: undefined,
    houseId: '',
    repairItemId: '',
    mobile: '',
    appointment: '',
    description: '',
    attachment: []
  },
  openHouseLayer () {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer () {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer () {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer () {
    this.setData({
      repairLayerVisible: false
    })
  },

  openDateLayer () {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer () {
    this.setData({ dateLayerVisible: false })
  },
  goList () {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index'
    })
  },
  onLoad (query) {
    this.query = query
    this.getList()
    this.getRepairList()
    if (this.query.id) {
      // 有id是编辑
      this.getDetail()
      wx.setNavigationBarTitle({
        title: '编辑保修信息'
      })
    } else {
      // 没id是新增
      wx.setNavigationBarTitle({
        title: '添加保修信息'
      })
    }
  },
  // 获取房屋列表
  async getList () {
    const { data } = await wx.http.get('/house')
    console.log(data)
    this.setData({
      houseList: data
    })
  },
  // 获取报修项目
  async getRepairList () {
    const { data } = await wx.http.get('/repairItem')
    console.log(data)
    this.setData({
      repairItem: data
    })
  },
  // 选择房屋
  selectHouse (e) {
    console.log(e)
    this.setData({
      houseName: e.detail.name,
      houseId: e.detail.id
    })
  },
  // 选择报修项目
  selectRepair (e) {
    console.log(e)
    this.setData({
      repairName: e.detail.name,
      repairItemId: e.detail.id
    })
  },
  // 选择日期
  confirmDate (e) {
    // const date = new Date(e.detail)
    this.setData({
      // appointment: date.toLocaleDateString('zh-CN').replaceAll('/', '-'),
      appointment: wx.utils.formatDate(e.detail),
      dateLayerVisible: false
    })
  },
  // 选择文件后触发
  async afterRead (e) {
    const { data } = await wx.http.upload('/upload', {
      name: 'file',
      filePath: e.detail.file.tempFilePath
    })
    this.setData({
      attachment: [...this.data.attachment, data]
    })
  },
  // 删除图片
  deleteImg (e) {
    this.setData({
      attachment: this.data.attachment.filter((item, index) => index !== e.detail.index)
    })
  },
  beforeRead (e) {
    const { file, callback } = e.detail
    if (file.size > 1024 * 1024 * 2) {
      wx.utils.toast('文件过大！')
      return callback(false)
    }
    callback(true)
  },
  // 提交
  async onSubmit () {
    const {
      // eslint-disable-next-line no-unused-vars
      currentDate,
      // eslint-disable-next-line no-unused-vars
      dateLayerVisible,
      // eslint-disable-next-line no-unused-vars
      houseLayerVisible,
      // eslint-disable-next-line no-unused-vars
      houseList,
      // eslint-disable-next-line no-unused-vars
      repairItem,
      // eslint-disable-next-line no-unused-vars
      repairLayerVisible,
      // eslint-disable-next-line no-unused-vars
      repairName,
      // eslint-disable-next-line no-unused-vars
      houseName,
      // eslint-disable-next-line no-unused-vars
      __webviewId__,
      ...body
    } = this.data
    console.log(body)
    await wx.http.post('/repair', body)
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index'
    })
  },
  // 获取详情
  async getDetail () {
    const { data } = await wx.http.get(`/repair/${this.query.id}`)
    console.log(data)
    this.setData({
      ...data,
      repairName: data.repairItemName,
      houseName: data.houseInfo
    })
  }
})
